package com.jxdinfo.hussar.example.common.dict.controller;

import com.jxdinfo.hussar.example.common.annotation.Log;
import com.jxdinfo.hussar.example.common.core.controller.BaseController;
import com.jxdinfo.hussar.example.common.core.domain.AjaxResult;
import com.jxdinfo.hussar.example.common.core.page.TableDataInfo;
import com.jxdinfo.hussar.example.common.enums.BusinessType;
import com.jxdinfo.hussar.example.common.utils.StringUtils;
import com.jxdinfo.hussar.example.common.utils.poi.ExcelUtil;
import com.jxdinfo.hussar.example.common.dict.domain.SysDictSingle;
import com.jxdinfo.hussar.example.common.dict.domain.SysDictType;
import com.jxdinfo.hussar.example.common.dict.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 字典类型Controller
 *
 * @author ruoyi
 * @date 2024-03-21
 */
@RestController
@RequestMapping("/Dict/SysDictType")
public class SysDictTypeController extends BaseController
{
    @Autowired
    private ISysDictTypeService sysDictTypeService;

    /**
     * 刷新字典缓存
     */
//    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
//    @GetMapping("/refreshCache")
//    public AjaxResult refreshCache()
//    {
//        sysDictTypeService.resetDictCache();
//        return AjaxResult.success();
//    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<SysDictType> dictTypes = sysDictTypeService.selectDictTypeAll();
        return AjaxResult.success(dictTypes);
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type")
    public AjaxResult dictType(@RequestParam(value = "0") String[] dictType)
    {
        List<SysDictSingle> data = null;
        for (String s : dictType) {
            data = sysDictTypeService.selectDictDataByType(s);
            if (StringUtils.isNull(data))
            {
                data = new ArrayList<SysDictSingle>();
            }
        }
        return AjaxResult.success(data);
    }

    /**
     * 查询字典类型列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysDictType sysDictType)
    {
        startPage();
        List<SysDictType> list = sysDictTypeService.selectSysDictTypeList(sysDictType);
        return getDataTable(list);
    }

    /**
     * 导出字典类型列表
     */
    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictType sysDictType)
    {
        List<SysDictType> list = sysDictTypeService.selectSysDictTypeList(sysDictType);
        ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
        util.exportExcel(response, list, "字典类型数据");
    }

    /**
     * 获取字典类型详细信息
     */
    @GetMapping(value = "/{typeId}")
    public AjaxResult getInfo(@PathVariable("typeId") Long typeId)
    {
        return AjaxResult.success(sysDictTypeService.selectSysDictTypeByTypeId(typeId));
    }

    /**
     * 新增字典类型
     */
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDictType sysDictType)
    {
        return toAjax(sysDictTypeService.insertSysDictType(sysDictType));
    }

    /**
     * 修改字典类型
     */
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDictType sysDictType)
    {
        return toAjax(sysDictTypeService.updateSysDictType(sysDictType));
    }

    /**
     * 删除字典类型
     */
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{typeIds}")
    public AjaxResult remove(@PathVariable Long[] typeIds)
    {
        return toAjax(sysDictTypeService.deleteSysDictTypeByTypeIds(typeIds));
    }
}
